#!/bin/bash

# 设置日期格式为YYYYMMDD
DATE=$(date +"%Y%m%d")

echo "====================================================="
echo "HeyGem Docker镜像构建与部署脚本"
echo "日期标签: ${DATE}"
echo "====================================================="

# 询问是否执行构建和推送
read -p "是否构建并推送新镜像? (y/n): " BUILD_CHOICE
if [[ "$BUILD_CHOICE" =~ ^[Yy]$ ]]; then
    echo "开始构建和推送镜像..."
    ./build_push_image.sh
    
    # 检查构建结果
    if [ $? -ne 0 ]; then
        echo "构建或推送失败，部署终止"
        exit 1
    fi
else
    echo "跳过构建和推送步骤"
fi

# 询问是否更新compose文件
read -p "是否更新docker-compose.yml文件中的镜像版本? (y/n): " UPDATE_CHOICE
if [[ "$UPDATE_CHOICE" =~ ^[Yy]$ ]]; then
    echo "开始更新docker-compose.yml..."
    ./update_compose.sh
    
    # 检查更新结果
    if [ $? -ne 0 ]; then
        echo "更新docker-compose.yml失败，部署终止"
        exit 1
    fi
else
    echo "跳过更新docker-compose.yml步骤"
fi

# 询问是否重启服务
read -p "是否重启服务以应用新镜像? (y/n): " RESTART_CHOICE
if [[ "$RESTART_CHOICE" =~ ^[Yy]$ ]]; then
    echo "正在重启服务..."
    docker-compose down
    docker-compose up -d
    
    # 检查服务状态
    echo "服务状态:"
    docker-compose ps
    
    echo "====================================================="
    echo "部署完成!"
    echo "新镜像版本: registry.cn-beijing.aliyuncs.com/dolphi/heygem_dolphi:${DATE}"
    echo "====================================================="
else
    echo "跳过重启服务步骤"
    echo "====================================================="
    echo "配置更新完成，请手动运行 'docker-compose up -d' 应用更改"
    echo "====================================================="
fi 